package com.cskaoyan.controller;

import com.cskaoyan.bean.bo.Goods_list;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.MarketGoods_list;
import com.cskaoyan.bean.marketBean.*;
import com.cskaoyan.bean.po.ZYL_admin_comment_detail;
import com.cskaoyan.bean.po.ZYL_admin_goods_update;
import com.cskaoyan.bean.vo.ZYL_admin_goods_catAndBrandVO;
import com.cskaoyan.bean.vo.ZYL_admin_goods_deleteVO;
import com.cskaoyan.service.MarketGoods_zyl_Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * zyl 商品管理
 *
 * @author 朱尤亮
 * @date 2022/12/28
 */

@RestController
@RequestMapping("admin/goods")
public class ZylCommoditymanagementController {

    /**
     * 商品管理zyl服务
     */
    @Autowired
    MarketGoods_zyl_Service marketGoodsZylService;

    /**
     * 商品回显
     *
     * @param map
     * @return {@link MarketGoods_list}
     */

    @RequestMapping("list")
    public MarketGoods_list list(Goods_list map) {
        if (map.getPage()==null) {
            MarketGoods_list marketGoods_list = new MarketGoods_list();
            marketGoods_list.equals(0);
            marketGoods_list.setErrmsg("成功");
            return marketGoods_list;
        }
        PageHelper.startPage(map.getPage(), map.getLimit());
        List<MarketGoods> goods = marketGoodsZylService.list(map);
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(goods);
        Integer total = Math.toIntExact(pageInfo.getTotal());
        int pages;
        if (total % map.getLimit() > 0) {
            pages = (int) (total / map.getLimit() + 1);
        } else {
            pages = (int) (total / map.getLimit());
        }
        MarketGoods_list.DataDTO dataDTO = new MarketGoods_list.DataDTO(total, pages, map.getLimit(), map.getPage(), goods);
        MarketGoods_list marketGoods_list = new MarketGoods_list(0, dataDTO, "成功");
        return marketGoods_list;
    }


    @PostMapping("update")
    public BaseRespVo update(@RequestBody ZYL_admin_goods_update zyl_admin_goods_update) {
        BaseRespVo baseRespVo = new BaseRespVo();

        try {
            if (zyl_admin_goods_update.getGoods().getGoodsSn() == null) {
                baseRespVo.setErrno(404);
                baseRespVo.setErrmsg("检查参数！");
                return baseRespVo;
            }
            List<MarketGoodsAttribute> attributes = zyl_admin_goods_update.getAttributes();
            for (MarketGoodsAttribute attribute : attributes) {
                //MarketGoodsAttributeExample example = new MarketGoodsAttributeExample();
                //MarketGoodsAttributeExample.Criteria criteria = example.createCriteria();
                //MarketGoodsAttributeExample.Criteria criteria1 = criteria.andIdEqualTo(attribute.getId());
                marketGoodsZylService.updateByPrimaryKey(attribute);
            }
            MarketGoods goods = zyl_admin_goods_update.getGoods();
            marketGoodsZylService.updateByPrimaryKey(goods);
            List<MarketGoodsSpecification> specifications = zyl_admin_goods_update.getSpecifications();
            List<MarketGoodsProduct> products = zyl_admin_goods_update.getProducts();
            for (MarketGoodsProduct product : products) {
                marketGoodsZylService.updateByPrimaryKey(product);
            }
            for (MarketGoodsSpecification specification : specifications) {
                marketGoodsZylService.updateByPrimaryKey(specification);
            }
            baseRespVo.setErrno(0);
            baseRespVo.setErrmsg("成功");
        } catch (Exception e) {
            baseRespVo.setErrno(404);
            baseRespVo.setErrmsg("检查参数！");
        } finally {
            return baseRespVo;
        }

    }

    @PostMapping("create")
    public BaseRespVo create(@RequestBody ZYL_admin_goods_update zyl_admin_goods_update) {
        BaseRespVo baseRespVo = new BaseRespVo();
        try {

            if (zyl_admin_goods_update.getGoods().getGoodsSn() == null || zyl_admin_goods_update.getGoods().getCategoryId() == null) {
                baseRespVo.setErrno(404);
                baseRespVo.setErrmsg("检查参数！");
                return baseRespVo;
            }
            MarketGoods goods = zyl_admin_goods_update.getGoods();
            marketGoodsZylService.insert(goods);
            List<MarketGoodsAttribute> attributes = zyl_admin_goods_update.getAttributes();
            for (MarketGoodsAttribute attribute : attributes) {
                attribute.setGoodsId(Integer.valueOf(goods.getGoodsSn()));
                //MarketGoodsAttributeExample example = new MarketGoodsAttributeExample();
                //MarketGoodsAttributeExample.Criteria criteria = example.createCriteria();
                //MarketGoodsAttributeExample.Criteria criteria1 = criteria.andIdEqualTo(attribute.getId());
                marketGoodsZylService.insert(attribute);
            }
            List<MarketGoodsSpecification> specifications = zyl_admin_goods_update.getSpecifications();
            List<MarketGoodsProduct> products = zyl_admin_goods_update.getProducts();
            for (MarketGoodsProduct product : products) {
                product.setGoodsId(Integer.valueOf(goods.getGoodsSn()));
                marketGoodsZylService.insert(product);
            }
            for (MarketGoodsSpecification specification : specifications) {
                specification.setGoodsId(Integer.valueOf(goods.getGoodsSn()));
                marketGoodsZylService.insert(specification);
            }
            baseRespVo.setErrno(0);
            baseRespVo.setErrmsg("成功");
        } catch (Exception e) {
            baseRespVo.setErrno(404);
            baseRespVo.setErrmsg("检查参数！");
        } finally {
            return baseRespVo;
        }


    }

    /**
     * 商品删除
     *
     * @param zyl_admin_goods_deleteVO
     * @return {@link ZYL_admin_goods_deleteVO}
     */

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody ZYL_admin_goods_deleteVO zyl_admin_goods_deleteVO) {
        System.out.println("zyl_admin_goods_deleteVO = " + zyl_admin_goods_deleteVO);
        BaseRespVo baseRespVo = new BaseRespVo();
        try {
            marketGoodsZylService.delete_zyl_goods(zyl_admin_goods_deleteVO.getId());
            marketGoodsZylService.delete_zyl_goods_attribbute(zyl_admin_goods_deleteVO.getId());
            marketGoodsZylService.delete_zyl_goods_product(zyl_admin_goods_deleteVO.getId());
            marketGoodsZylService.delete_zyl_goods_specification(zyl_admin_goods_deleteVO.getId());
            baseRespVo.setErrno(0);
            baseRespVo.setErrmsg("成功");
        } catch (Exception e) {
            return baseRespVo.exception("数据库异常");
        } finally {
            return baseRespVo;
        }
    }


    @RequestMapping("catAndBrand")
    public ZYL_admin_goods_catAndBrandVO catAndBrand() {
        List<MarketBrand> marketBrands = marketGoodsZylService.selectByExample(new MarketBrandExample());
        List<MarketCategory_zyl> catAndCategory = new ArrayList<MarketCategory_zyl>();
        for (MarketBrand marketBrand : marketBrands) {
            MarketCategory_zyl marketCategory_zyl = new MarketCategory_zyl(marketBrand.getName(), marketBrand.getId());
            catAndCategory.add(marketCategory_zyl);
        }

        ZYL_admin_goods_catAndBrandVO zyl_admin_goods_catAndBrandVO = new ZYL_admin_goods_catAndBrandVO();
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.setOrderByClause("pid");
        List<MarketCategory> marketCategories = marketGoodsZylService.selectByExample(marketCategoryExample);
        List<MarketCategory_zyl> marketCategory_zyls = marketGoodsZylService.selectByExample_list_zyl();
        /*for (MarketCategory_zyl marketCategory_zyl : marketCategory_zyls) {
            System.out.println("marketCategory_zyl = " + marketCategory_zyl);
        }*/
        List<ZYL_admin_goods_catAndBrandVO.DataDTO.CategoryListDTO> categoryListDTO = new ArrayList<>();
        for (MarketCategory_zyl marketCategory : marketCategory_zyls) {
            Integer pid = marketCategory.getValue();
            List<MarketCategory_zyl> marketCategory_zylss = marketGoodsZylService.selectByPrimaryKey_goods_zyl(pid);
            ZYL_admin_goods_catAndBrandVO.DataDTO.CategoryListDTO categoryListDTO1 = new ZYL_admin_goods_catAndBrandVO.DataDTO.CategoryListDTO();
            categoryListDTO1.setValue(marketCategory.getValue());
            categoryListDTO1.setLabel(marketCategory.getLabel());
            categoryListDTO1.setChildren(marketCategory_zylss);
            categoryListDTO.add(categoryListDTO1);
        }
        // List<MarketCategory_zyl> marketCategory_zyls = marketGoodsZylService.selectByExample_list_zyl();
        ZYL_admin_goods_catAndBrandVO.DataDTO dataDTO = new ZYL_admin_goods_catAndBrandVO.DataDTO(categoryListDTO, catAndCategory);
        zyl_admin_goods_catAndBrandVO.setData(dataDTO);
        zyl_admin_goods_catAndBrandVO.setErrno(0);
        zyl_admin_goods_catAndBrandVO.setErrmsg("成功");
        return zyl_admin_goods_catAndBrandVO;
    }


    @RequestMapping("detail")

    public BaseRespVo detail(Integer id) {
        BaseRespVo baseRespVo = new BaseRespVo();
        MarketGoodsAttributeExample example = new MarketGoodsAttributeExample();
        MarketGoodsProductExample example1 = new MarketGoodsProductExample();
        MarketGoodsSpecificationExample example2 = new MarketGoodsSpecificationExample();
        MarketGoodsAttributeExample.Criteria criteria = example.createCriteria();
        MarketGoodsProductExample.Criteria criteria1 = example1.createCriteria();
        MarketGoodsSpecificationExample.Criteria criteria2 = example2.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        criteria1.andGoodsIdEqualTo(id);
        criteria2.andGoodsIdEqualTo(id);
        List<MarketGoodsAttribute> marketGoodsAttributes = marketGoodsZylService.selectByExampleAttribute(example);
        MarketGoods g = marketGoodsZylService.selectByPrimaryKey(id);
        MarketCategory marketCategory = marketGoodsZylService.selectByPrimaryKey_zyl(g.getCategoryId());
      /*  String gallery = g.getGallery();
        String str2 = "[";
        String str3 = "]";
        String str4 = "\"";
        String s = trimStr(gallery, str2);
        String s1 = trimStr(s, str3);
        String s2 = trimStr(s1, str4);
        String substring = s2.replace("\"", "");
        System.out.println("s3 = " + substring);
        String[] strings = substring.split(",");
        List<String> gallerys = new ArrayList();
        for (int i = 0; i < strings.length; i++) {
            gallerys.add(strings[i]);
        }*/
       /* MarketGoods_s marketGoods_s = new MarketGoods_s(g.getId(), g.getGoodsSn(), g.getName(), g.getCategoryId(), g.getBrandId(), g.getGallery(),
                g.getKeywords(), g.getBrief(), g.getIsOnSale(), g.getSortOrder(), g.getPicUrl(), g.getShareUrl(), g.getIsNew(), g.getIsHot(),
                g.getUnit(), g.getCounterPrice(), g.getRetailPrice(), g.getAddTime(), g.getUpdateTime(), g.getDeleted(), g.getDetail());*/
        List<MarketGoodsProduct> marketGoodsAttributes1 = marketGoodsZylService.selectByExampleProduct(example1);
        List<MarketGoodsSpecification> marketGoodsAttributes2 = marketGoodsZylService.selectByExampleSpecification(example2);
        Integer[] categoryIds = new Integer[]{marketCategory.getPid(), marketCategory.getId()};
        /*categoryIds.add(0, g.getCategoryId());
        categoryIds.add(1, g.getBrandId());*/
        ZYL_admin_comment_detail data = new ZYL_admin_comment_detail();
        data.setAttributes(marketGoodsAttributes);
        data.setSpecifications(marketGoodsAttributes2);
        data.setProducts(marketGoodsAttributes1);
        data.setCategoryIds(categoryIds);
        data.setGoods(g);
        baseRespVo.setErrno(0);
        baseRespVo.setData(data);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    public static String trimStr(String str, String indexStr) {
        if (str == null) {
            return null;
        }
        StringBuilder newStr = new StringBuilder(str);
        if (newStr.indexOf(indexStr) == 0) {
            newStr = new StringBuilder(newStr.substring(indexStr.length()));

        } else if (newStr.indexOf(indexStr) == newStr.length() - indexStr.length()) {
            newStr = new StringBuilder(newStr.substring(0, newStr.lastIndexOf(indexStr)));//在结尾

        } else if (newStr.indexOf(indexStr) < (newStr.length() - indexStr.length())) {
            newStr = new StringBuilder(newStr.substring(0, newStr.indexOf(indexStr))
                    + newStr.substring(newStr.indexOf(indexStr) + indexStr.length(), newStr.length()));

        }
        return newStr.toString();
    }

}




